import React from 'react';
import { Iframe } from 'mp-package-base';

if (window.Base) {
  window.Base.Attachments.onPreview = (modal, ext, src) => {
    if (['XLS', 'XLSX', 'DOC', 'DOCX', 'PPT', 'PPTX'].includes(ext.toUpperCase())) {
      modal.open({
        width: '90%',
        bodyStyle: { height: 'calc(100vh - 60px)' },
        content: <OfficeLiveViewer url={src} embed />,
        footer: null,
      });
    } else {
      window.Base.alert("暂不支持该文件格式的在线预览");
    }
  };
}

export default function OfficeLiveViewer({ embed = false, url = '' }) {
  if (!url.startsWith('http')) url = window.location.origin + url;
  return <Iframe src={`https://view.officeapps.live.com/op/${embed ? 'embed' : 'view'}.aspx?src=${encodeURIComponent(url)}`} />;
}